package com.jcl.cloud.services.crawler.search.util;

import org.apache.http.impl.client.BasicCookieStore;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class SerializeUtils {

    private static Logger logger = LoggerFactory.getLogger(SerializeUtils.class);

    public static String serializeToStr(BasicCookieStore cookieStore) throws IOException {
        String result = "";

        ByteArrayOutputStream byteArrayOutputStream = null;
        ObjectOutputStream objectOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(cookieStore);
            result = byteArrayOutputStream.toString("ISO-8859-1");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                objectOutputStream.close();
                byteArrayOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }
    public static BasicCookieStore serializeToObj(String objStr) {
        ByteArrayInputStream byteArrayInputStream = null;
        ObjectInputStream objectInputStream = null;
        BasicCookieStore cookieStore = null;
        try {
            byteArrayInputStream = new ByteArrayInputStream(objStr.getBytes("ISO-8859-1"));
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            cookieStore = (BasicCookieStore) objectInputStream.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }  finally {
            try {
                objectInputStream.close();
                byteArrayInputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return cookieStore;
    }
}